package com.clkj.tramcarlibrary.service;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.amap.api.location.AMapLocation;

public class MyLocationManager {

	private static Context mContext;
	private LocationManager gpsLocationManager;
	private static final int MINTIME = 2000;
	private static final int MININSTANCE = 10;
	private static MyLocationManager instance;
	private Location lastLocation = null;
	private LocationCallBack mCallback;
	private AMapLocation aMapLocation;

	// public void init(Context c, LocationCallBack callback) {
	// mContext = c;
	// this.mCallback = callback;
	// this.aMapLocation = new AMapLocation("GPS");
	// }

	private MyLocationManager() {
	}

	public void requestLocation() {
		// Gps 定位
		this.gpsLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		// Location gpsLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (this.gpsLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
			this.gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINTIME, MININSTANCE, this.locationListener);
		}
		// 基站定位
		// this.networkLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		// // Location networkLocation = gpsLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (this.gpsLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
			this.gpsLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE, this.locationListener);
		}
	}

	public static MyLocationManager getInstance() {
		if (null == instance) {
			instance = new MyLocationManager();
		}
		return instance;
	}

	public void init(Context c) {
		mContext = c;
		this.aMapLocation = new AMapLocation("GPS");
	}

	public void registerCallback(LocationCallBack callback) {
		this.mCallback = callback;
	}

	public Location requestLastLocation() {
		Location gpsLocation = this.gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		Location networkLocation = this.gpsLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (gpsLocation != null) {
			return gpsLocation;
		}
		if (networkLocation != null) {
			return networkLocation;
		}
		return null;
	}

	public void unregisterListener() {
		this.mCallback = null;
	}

	private void updateLocation(Location location) {
		if (location != null) {
			if (this.lastLocation == null) {
				this.lastLocation = location;
			} else {
				this.lastLocation.set(location);
			}
			if (this.mCallback != null) {
				this.mCallback.onCurrentLocation(location);
			}
			this.aMapLocation.setLatitude(location.getLatitude());
			this.aMapLocation.setLongitude(location.getLongitude());
			this.aMapLocation.setAccuracy(location.getAccuracy());
			// LocationRecorder.getInstance().notifyLocationChanged(this.aMapLocation);
		}
	}

	private final LocationListener locationListener = new LocationListener() {

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onProviderDisabled(String provider) {
		}

		public void onLocationChanged(Location location) {
			updateLocation(location);
		}
	};

	public Location getMyLocation() {
		return this.lastLocation;
	}

	private static int ENOUGH_LONG = 1000 * 60;

	public interface LocationCallBack {

		/**
		 * 当前位置
		 * @param location
		 */
		void onCurrentLocation(Location location);
	}

	public void destoryLocationManager() {
		System.out.println("stop device service");
		if (this.gpsLocationManager != null) {
			this.gpsLocationManager.removeUpdates(this.locationListener);
		}
		// networkLocationManager.removeUpdates(locationListener);
	}
}
